#include <iostream>
#include <vector>
#include <string.h>
#include <algorithm>

using namespace std;

class Solution {
public:
    string licenseKeyFormatting(string s, int k)
    {
      string res;
      int cnt = 0;
      for (int idx = s.length() - 1; idx >= 0; idx--) {
         if (s[idx] != '-') {
            res.push_back(toupper(s[idx]));
            cnt++;
            if (cnt == k) {
               res.push_back('-');
               cnt = 0;
            }
         }
      }
      if ((res.length() > 0) && (res.back() == '-')) {
         res.pop_back();
      }
      reverse(res.begin(), res.end());
      return res;
   }
};

int main()
{
   Solution s;
   string str{"5-fdd"};
   string res = s.licenseKeyFormatting(str, 2);
   cout << res << endl;

   return 0;
}

